package com.langshixiaobai.mqdemo.server.datacenter;

import com.langshixiaobai.mqdemo.MqDemoApplication;
import com.langshixiaobai.mqdemo.server.core.Binding;
import com.langshixiaobai.mqdemo.server.core.Exchange;
import com.langshixiaobai.mqdemo.server.core.ExchangeType;
import com.langshixiaobai.mqdemo.server.core.MSGQueue;
import com.langshixiaobai.mqdemo.server.mapper.MetaMapper;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.List;

//后续进行手动管理，所以不用@Autowired
@Slf4j
public class DataBaseManager {

    //手动获取
    private MetaMapper mapper;

    //构造方法适合简单初始化
    //进行业务初始化
    public void init() {
        //通过上下文对象，获取bean
        mapper = MqDemoApplication.context.getBean(MetaMapper.class);

        //检查是否存在，存在跳过，不存在则创建对应数据库
        if (!checkDBExists()) {
            //创建目录
            File file = new File("./data");
            file.mkdirs();
            //创建数据库
            createDB();
            //创建默认交换机
            createDefaultExchange();
            System.out.println("数据meta.db已经初始化完成");
        } else {
            System.out.println("数据库meta.db已经存在了");
        }
    }

    //meta.db数据库文件不存在时，Mybatis会自动将表进行创建
    //当创建表时，Mybatis会自动将数据库文件创建
    private void createDB() {
        //创建Binding表
        mapper.createBindingTable();
        //创建Exchange表
        mapper.createExchangeTable();
        //创建队列表
        mapper.createQueueTable();
        //打印
        System.out.println("表创建完成");

    }

    //检查是否存在文件
    private boolean checkDBExists() {
        // 检查路径是否存在文件
        File file = new File("./data/meta.db");
        if (file.exists()) {
            return true;
        }
        return false;
    }

    //RabbitMQ中存在默认的交换机，当发送消息不指定交换机名字时，就会使用这个默认交换机
    //所以这里也同样实现默认交换机，默认交换机类型为Direct直连交换机
    public void createDefaultExchange() {
        Exchange exchange = new Exchange();
        //设置名字
        exchange.setName("");
        //设置交换机类型为直连
        exchange.setType(ExchangeType.DIRECT);
        //设置持久化
        exchange.setDurable(true);
        //设置是否自动删除
        exchange.setAutoDelete(false);
        //数据库中写入默认交换机
        mapper.insertExchange(exchange);
        //打印
        System.out.println("创建默认交换机");
    }

    //删除数据库文件
    public void deleteDB(){
        File file = new File("./data/meta.db");
        File dic = new File("./data");
        boolean ret = file.delete();
        boolean ret1 = dic.delete();
        if(ret){
           log.info("{DataBaseMessage}删除数据库文件成功");
        }else{
            log.info("{DataBaseMessage}删除数据库文件失败");
        }
        if(ret1){
            log.info("{DataBaseMessage}删除数据库目录成功");
        }else{
            log.info("{DataBaseMessage}删除数据库目录失败");
        }


    }

    public void insertExchange(Exchange exchange) {
        mapper.insertExchange(exchange);
    }

    public void insertQueue(MSGQueue queue) {
        mapper.insertQueue(queue);
    }

    public void insertBinding(Binding binding) {
        mapper.insertBinding(binding);
    }

    public List<Exchange> findAllExchange() {
        return mapper.selectAllExchange();
    }

    public List<MSGQueue> findAllQueue() {
        return mapper.selectAllQueue();
    }

    public List<Binding> findAllBinding() {
        return mapper.selectAllBinding();
    }


    public void deleteExchange(String name){
        mapper.deleteExchange(name);
    }

    public void deleteQueue(String name){
        mapper.deleteQueue(name);
    }

    public void deleteBinding(Binding binding){
        mapper.deleteBinding(binding);
    }





}
